package com.game.eggscatcher.GameWorld;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.utils.Array;
import com.game.eggscatcher.GameHelpers.AssetLoader;
import com.game.eggscatcher.GameObjects.GiaCam;
import com.game.eggscatcher.GameObjects.Ro;


public class GameRenderer {
	private GameWorld myWorld;
	private OrthographicCamera cam;
	private ShapeRenderer shapeRenderer;
	private SpriteBatch batcher;
	private Array<GiaCam> ga = new Array<GiaCam>();
	private Array<GiaCam> vit = new Array<GiaCam>();
	private Array<GiaCam> ngan = new Array<GiaCam>();
	private GiaCam tempGC;
	private Ro ro;
	private final int M = 16;
	public GameRenderer(GameWorld world) {
		myWorld = world;
		initObjects();
		cam = new OrthographicCamera();
		cam.setToOrtho(true, 136, 204);
		shapeRenderer = new ShapeRenderer();
		shapeRenderer.setProjectionMatrix(cam.combined);
		batcher = new SpriteBatch();
		batcher.setProjectionMatrix(cam.combined);
	}
	private void initObjects(){
//		ga = myWorld.getGa();
//		vit = myWorld.getVit();
		ga = myWorld.getListGa();
		vit = myWorld.getListVit();
		ngan = myWorld.getListNgan();
		ro = myWorld.getRo();
	}
	public void render() {
		// TODO Auto-generated method stub
		Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
        batcher.begin();
        
        if (myWorld.isRunning()) {
        	drawElements();
        }
        
        if (myWorld.isReady()) {
        	drawElements();
        	drawReady();
        }
        if (myWorld.isPause()) {
        	drawElements();
        	drawPause();
        }
        if (myWorld.isGameOver()) {
        	drawElements();
        	drawGameOver();
        	drawReady();
        }
        if (myWorld.isLevelUp()) {
        	drawElements();
        	drawLevelUp();
        	drawReady();
        }
        if (myWorld.isHelp()) {
        	drawHelp();
        }
        batcher.end();
        
        
	}
	
	private void drawElements() {
		drawRo();
        drawGiaCam(AssetLoader.ga, ga);
        drawGiaCam(AssetLoader.vit, vit);
        drawGiaCam(AssetLoader.ngan, ngan);
        drawMisc();
	}
	private void drawReady() {
		AssetLoader.text.setScale(.14f, -.14f);
		AssetLoader.text.setColor(Color.WHITE);
		AssetLoader.text.draw(batcher, "Press h for Help", 31, 150);
		AssetLoader.text.setScale(.15f, -.15f);
		AssetLoader.text.setColor(Color.GREEN);
    	AssetLoader.text.draw(batcher, "Click (Enter) to Start", 16, 125);
	}
	private void drawPause() {
		AssetLoader.text.setScale(.20f, -.20f);
		AssetLoader.text.setColor(Color.WHITE);
    	AssetLoader.text.draw(batcher, "PAUSE", 48, 100);
    	
		AssetLoader.text.setScale(.15f, -.15f);
		AssetLoader.text.setColor(Color.GREEN);
    	AssetLoader.text.draw(batcher, "Click (Enter) to Cont.", 16, 125);
	}
	
	private void drawHelp() {
		AssetLoader.text.setScale(.14f, -.14f);
		AssetLoader.text.setColor(Color.WHITE);
    	AssetLoader.text.draw(batcher, "Mouse", 16, 28);
    	AssetLoader.text.draw(batcher, "Keyboard", 16, 58);
    	
    	AssetLoader.text.setScale(.10f, -.10f);
    	AssetLoader.text.setColor(Color.GREEN);
    	AssetLoader.text.draw(batcher, "Catch as many Eggs as possible.", 16, 10);
    	AssetLoader.text.draw(batcher, "Don't catch Shits.", 16, 18);
    	
		AssetLoader.text.setColor(Color.PINK);
    	AssetLoader.text.draw(batcher, "Click to start game.", 24, 40);
    	AssetLoader.text.draw(batcher, "Move to control the basket.", 24, 48);
    	
    	AssetLoader.text.draw(batcher, "Enter : start game", 24, 56 + M);
    	AssetLoader.text.draw(batcher, "<- : move left", 24, 64+ M);
    	AssetLoader.text.draw(batcher, "-> : move right", 24, 72+ M);
    	AssetLoader.text.draw(batcher, "g  : add Ga", 24, 80+ M);
    	AssetLoader.text.draw(batcher, "v  : add Vit", 24, 88+ M);
    	AssetLoader.text.draw(batcher, "n  : add Ngan", 24, 96+ M);
    	AssetLoader.text.draw(batcher, "r  : reset Highscore", 24, 104+ M);
    	AssetLoader.text.draw(batcher, "p  : pause", 24, 112+ M);
    	AssetLoader.text.draw(batcher, "h  : help", 24, 120+ M);
    	
		AssetLoader.text.setScale(.15f, -.15f);
		AssetLoader.text.setColor(Color.GREEN);
    	AssetLoader.text.draw(batcher, "Click (Enter) to Cont.", 16, 158);
    	
    	AssetLoader.handyText.setScale(.10f, -.10f);
		AssetLoader.handyText.setColor(Color.WHITE);
    	AssetLoader.handyText.draw(batcher, "Creator: Vu Trong Hung", 40, 195);
	}
	
	private void drawGameOver() {
		int highScore = AssetLoader.getHighScore();
		int length = ("" + myWorld.getCurrentScore()).length();
		AssetLoader.text.setScale(.14f, -.14f);
		AssetLoader.text.setColor(Color.PINK);
		AssetLoader.text.draw(batcher, "Your Score: " + myWorld.getCurrentScore(), 40 - (2 * length) , 73);
		length = ("" + highScore).length();
		if (myWorld.isHighScore) {
			AssetLoader.text.setScale(.12f, -.12f);
			AssetLoader.text.setColor(Color.RED);
			AssetLoader.text.draw(batcher, "New High Score: " + highScore, 36 - (2 * length) , 85);
		} else {
			AssetLoader.text.setScale(.12f, -.12f);
			AssetLoader.text.setColor(Color.ORANGE);
			AssetLoader.text.draw(batcher, "High Score: " + highScore, 45 - (2 * length) , 85);
		}
		AssetLoader.text.setScale(.20f, -.20f);
		AssetLoader.text.setColor(Color.WHITE);
    	AssetLoader.text.draw(batcher, "GAME OVER", 31, 100);
	}
	
	private void drawLevelUp() {
		int length = ("" + myWorld.getLevel()).length();
		AssetLoader.text.setScale(.20f, -.20f);
		AssetLoader.text.setColor(Color.RED);
    	AssetLoader.text.draw(batcher, "Level " + myWorld.getLevel(), 49 - (3 * length) , 100);
	}
	
	private void drawMisc() {
		batcher.enableBlending();
		batcher.draw(AssetLoader.egg, 107, 2, 5, 6);
		batcher.draw(AssetLoader.shit, 123, 2, 5, 6);
		AssetLoader.text.setScale(.11f, -.11f);
        AssetLoader.text.draw(batcher, "Score: " + ro.getScore(), 2, 0);
        AssetLoader.text.setColor(Color.WHITE);
        AssetLoader.text.draw(batcher, "Level: " + myWorld.getLevel(), 45, 0);
        AssetLoader.text.setColor(Color.GREEN);
        AssetLoader.text.draw(batcher, "Lives: " , 85, 0);
        AssetLoader.text.draw(batcher, ""+ro.getTrungLives(), 114, 0);
        AssetLoader.text.draw(batcher, ""+ro.getShitLives(), 130, 0);
	}
	private void drawRo() {
		batcher.enableBlending();
		batcher.draw(AssetLoader.ro, ro.getX(), ro.getY(), ro.WIDTH, ro.HEIGHT);
	}
	private void drawGiaCam(AtlasRegion skin, Array<GiaCam> giacam) {
		batcher.enableBlending();
		for (int j = 0; j < giacam.size; j++) {
			tempGC = giacam.get(j);
			batcher.draw(skin, tempGC.getX(), tempGC.getY(), tempGC.WIDTH, tempGC.HEIGHT);
			for (int i = 0; i < tempGC.getCountTrung(); i++) {
				if (tempGC.getTrung(i).alive) {
					if (tempGC.getTrung(i).isTrung) {
						batcher.draw(AssetLoader.egg, tempGC.getTrung(i).getX(), tempGC.getTrung(i).getY(), tempGC.getTrung(i).WIDTH, tempGC.getTrung(i).HEIGHT);
					} else {
						batcher.draw(AssetLoader.shit, tempGC.getTrung(i).getX(), tempGC.getTrung(i).getY(), tempGC.getTrung(i).WIDTH, tempGC.getTrung(i).HEIGHT);
					}
				}
			}
		}
		
	}

	

}
